package com.wuad.platformwuad.module.study.dateTimeDemo;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 获取当天，昨天，本周，上周，的起止时间戳
 *
 * @author ：Wuad
 * @date ：Created in 2021/4/7 15:27
 * @description :
 */
public class GetDateDemo {

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void main(String[] args) throws Exception {

        // 在当前日期上加上n个小时
        getDate_jia_hour();

        getDate_jia_minute();

        // 获取两个时间差值
//        getcha();


//        long timestamp = System.currentTimeMillis();
//        System.out.println("获取当前时间的时间戳格式数据：" + timestamp);

//        String newDate = DateUtil.format(getEndDayOfYear(),"yyyy-M-d");
//        System.out.println("当前年月日" + newDate);

//        System.out.println("返回指定格式的时间数据" + DateUtil.format(new Date(),"yyyyMMdd"));
//        int year = DateUtil.year(new Date());
//        System.out.println("当前年" + year);
//        int month = DateUtil.thisMonth();
//        System.out.println("当前月（1月是0）" + month);
//        System.out.println("根据当前时间生成数据id：" + getDateTime());
//        System.out.println("当天开始时间戳：" + getDayBegin().getTime());
//        System.out.println("当天结束时间戳：" + getDayEnd().getTime());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss格式的当天起始时间:" + getNowDayStart());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss格式的当天终止时间:" + getNowDayEnd());
//        System.out.println("昨天开始时间戳：" + getBeginDayOfYesterday());
//        System.out.println("昨天结束时间戳：" + getEndDayOfYesterDay());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-昨天开始时间：" + simpleDateFormat.format(getBeginDayOfYesterday()));
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-昨天结束时间：" + simpleDateFormat.format(getEndDayOfYesterDay()));
//        System.out.println("本周开始时间戳：" + getBeginDayOfWeek().getTime());
//        System.out.println("本周结束时间戳：" + getEndDayOfWeek().getTime());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本周开始时间：" + simpleDateFormat.format(getBeginDayOfWeek()));
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本周结束时间：" + simpleDateFormat.format(getEndDayOfWeek()));
//        System.out.println("上周起止时间戳" + getLastWeekTimeFrame());
//        System.out.println("本月开始时间戳：" + getBeginDayOfMonth().getTime());
//        System.out.println("本月结束时间戳：" + getEndDayOfMonth().getTime());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本月开始时间：" + simpleDateFormat.format(getBeginDayOfMonth()));
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本月结束时间：" + simpleDateFormat.format(getEndDayOfMonth()));
//        System.out.println("上月开始时间戳：" + getPreviousBeginDayOfMonth().getTime());
//        System.out.println("上月结束时间戳：" + getPreviousEndDayOfMonth().getTime());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-上月开始时间：" + simpleDateFormat.format(getPreviousBeginDayOfMonth()));
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-上月开始时间：" + simpleDateFormat.format(getPreviousEndDayOfMonth()));
//        System.out.println("本年开始时间戳：" + getBeginDayOfYear().getTime());
//        System.out.println("本年结束时间戳：" + getEndDayOfYear().getTime());
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本年开始时间：" + simpleDateFormat.format(getBeginDayOfYear()));
//        System.out.println("String类型-yyyy-MM-dd HH:mm:ss-本年结束时间：" + simpleDateFormat.format(getEndDayOfYear()));

//        System.out.println("获取过去n天的日期：" + getPastDate(7));
    }

    /**
     * 日期加
     */
    private static void getDate_jia_hour() {
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 往后加一个小时
        LocalDateTime next1 = localDateTime.plusHours(1);
        LocalDateTime next2 = localDateTime.plusHours(73);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowDateStr = localDateTime.format(formatter);
        String next1DateStr = next1.format(formatter);
        String next2DateStr = next2.format(formatter);
        System.out.println("当前日期时间：" + nowDateStr);
        System.out.println("当前日期时间+1小时：" + next1DateStr);
        System.out.println("当前日期时间+73小时：" + next2DateStr);
    }

    private static void getDate_jia_minute() {
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 往后加一个小时
        LocalDateTime next1 = localDateTime.plusMinutes(30);
        LocalDateTime next2 = localDateTime.plusMinutes(4350);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowDateStr = localDateTime.format(formatter);
        String next1DateStr = next1.format(formatter);
        String next2DateStr = next2.format(formatter);
        System.out.println("当前日期时间：" + nowDateStr);
        System.out.println("当前日期时间+半小时：" + next1DateStr);
        System.out.println("当前日期时间+72.5小时：" + next2DateStr);
    }


    private static long getcha() throws InterruptedException {
        Date a = new Date();
        long timestampA = System.currentTimeMillis();
        Thread.sleep(3000);
        Date b = new Date();
        long timestampB = System.currentTimeMillis();

        long interval = (b.getTime() - a.getTime())/1000;

        long interval2 = (timestampB - timestampA)/1000;

        System.out.println("两个时间相差：" + interval + "秒");
        System.out.println("两个时间相差：" + interval2 + "秒");
        return interval;
    }


    /**
     * 根据当前数据生成数据id
     * @return
     */
    private static String getDateTime(){
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 返回String类型的
     * yyyy-MM-dd HH:mm:ss
     * 当天的起始时间
     */
    public static String getNowDayStart() {
        //获取当前日期
        LocalDate nowDate = LocalDate.now();
        //设置零点
        LocalDateTime beginTime = LocalDateTime.of(nowDate, LocalTime.MIN);
        //将时间进行格式化
        String time1= beginTime.format(dtf);
        return time1;
    }

    /**
     * 返回String类型的
     * yyyy-MM-dd HH:mm:ss
     * 当天的终止时间
     */
    public static String getNowDayEnd() {
        //获取当前日期
        LocalDate nowDate = LocalDate.now();
        //设置当天的结束时间
        LocalDateTime endTime = LocalDateTime.of(nowDate,LocalTime.MAX);
        //将时间进行格式化
        String time2 =dtf.format(endTime);
        return time2;
    }

    /**
     * 获取当天开始时间的时间戳
     * @return
     */
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
//        System.out.println("这样也可以直接获取到时间戳，为了在其他方法中使用，这里就返回Date类型" + cal.getTimeInMillis());
        return cal.getTime();
    }

    /**
     * 获取当天结束时间的时间戳
     * @return
     */
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取昨天的开始时间的时间戳
     *
     * @return
     */
    public static long getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime().getTime();
    }

    /**
     * 获取昨天结束时间的时间戳
     * @return
     */
    public static long getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime().getTime();
    }

    /**
     * 获取本周开始时间
     * @return
     */
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        cal.getTime();
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本周的结束时间
     * @return
     */
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取上周的起止时间戳
     * @return
     */
    public static String getLastWeekTimeFrame() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        long end = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        long start = calendar.getTimeInMillis();
        return "start" + start + "," + "end:" + end;
    }

    /**
     * 获取本月的开始时间
     * @return
     */
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取本月的结束时间
     * @return
     */
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取上月的开始时间
     * @return
     */
    public static Date getPreviousBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取上月的结束时间
     * @return
     */
    public static Date getPreviousEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取本年的开始时间
     * @return
     */
    public static Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本年的结束时间
     * @return
     */
    public static Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    /**
     * 获取过去第n天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }


    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    //获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    //两个日期相减得到的天数
    public static int getDiffDays(Date beginDate, Date endDate) {

        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }

        long diff = (endDate.getTime() - beginDate.getTime())
                / (1000 * 60 * 60 * 24);

        int days = new Long(diff).intValue();

        return days;
    }

    //两个日期相减得到的毫秒数
    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    //获取两个日期中的最大日期
    public static Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    //获取两个日期中的最小日期
    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }

    //返回某月该季度的第一个月
    public static Date getFirstSeasonDate(Date date) {
        final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = SEASON[cal.get(Calendar.MONTH)];
        cal.set(Calendar.MONTH, sean * 3 - 3);
        return cal.getTime();
    }

    //返回某个日期下几天的日期
    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        return cal.getTime();
    }

    //返回某个日期前几天的日期
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }

    //获取某年某月到某年某月按天的切片日期集合（间隔天数的日期集合）
    public static List getTimeList(int beginYear, int beginMonth, int endYear,
                                   int endMonth, int k) {
        List list = new ArrayList();
        if (beginYear == endYear) {
            for (int j = beginMonth; j <= endMonth; j++) {
                list.add(getTimeList(beginYear, j, k));

            }
        } else {
            {
                for (int j = beginMonth; j < 12; j++) {
                    list.add(getTimeList(beginYear, j, k));
                }

                for (int i = beginYear + 1; i < endYear; i++) {
                    for (int j = 0; j < 12; j++) {
                        list.add(getTimeList(i, j, k));
                    }
                }
                for (int j = 0; j <= endMonth; j++) {
                    list.add(getTimeList(endYear, j, k));
                }
            }
        }
        return list;
    }

    //获取某年某月按天切片日期集合（某个月间隔多少天的日期集合）
    public static List getTimeList(int beginYear, int beginMonth, int k) {
        List list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(Calendar.DATE);
        for (int i = 1; i < max; i = i + k) {
            list.add(begincal.getTime());
            begincal.add(Calendar.DATE, k);
        }
        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }

    /**
     * 得到某年某周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);
        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,
                calendar.getFirstDayOfWeek()); // Sunday
        return calendar.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,
                calendar.getFirstDayOfWeek() + 6); // Saturday
        return calendar.getTime();
    }

}
